package com.example.demo.mapper;

import com.example.demo.model.Forum;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ForumMapper {
    @Select("SELECT * FROM forum")
    List<Forum> selectAllForums();

    @Select("SELECT * FROM forum WHERE id = #{id}")
    Forum selectForumById(Long id);

    @Select("SELECT * FROM forum WHERE username = #{username}")
    List<Forum> selectForumByUserName(String username);

    @Select("SELECT * FROM forum WHERE username = #{userid}")
    List<Forum> selectForumByUserId(Long userId);

    @Insert("INSERT INTO forum (parent_id, title, content, user_id, username, create_time, reply_time) " +
            "VALUES (#{parentId}, #{title}, #{content}, #{userId}, #{username}, #{createTime}, #{replyTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertForum(Forum forum);

    @Insert("DELETE FROM forum WHERE id = #{id}")
    void deleteForumById(Long id);

    @Insert("UPDATE forum SET title = #{title}, content = #{content}, reply_time = #{replyTime} WHERE id = #{id}")
    void updateForum(Forum forum);
}
